﻿using System.Collections.Generic;
using Godot;
using Newtonsoft.Json;

namespace GoGameClient.script.tool;

public partial class SerializeScript : Node2D
{
    private readonly string[] _nameBy8 = { "右下", "左下", "左上", "右上", "下", "左", "上", "右" };
    private readonly string[] _nameBy4 = { "右下", "左下", "左上", "右上" };
    private readonly string[] _fileNameByDefault = { "待战", "挨揍", "攻击", "施法", "死亡", "逃跑", "防御", "行走", "站立" };

    public override void _Ready()
    {
        QueryDir("res://resources/role/", 1);
    }

    private void QueryDir(string path, int type)
    {
        var dir = DirAccess.Open(path);
        if (dir == null) return;
        dir.ListDirBegin();
        var fileName = dir.GetNext();
        while (fileName != "")
        {
            if (dir.CurrentIsDir())
            {
                if (!fileName.Equals("模型"))
                {
                    if (type == 0)
                    {
                        Convert($"{dir.GetCurrentDir()}/{fileName}/模型", _fileNameByDefault);
                    }
                    else
                    {
                        Remove($"{dir.GetCurrentDir()}/{fileName}/模型", _fileNameByDefault);
                    }
                }

                GD.Print($"{dir.GetCurrentDir()}/{fileName}");
                QueryDir($"{dir.GetCurrentDir()}/{fileName}", type);
            }

            fileName = dir.GetNext();
        }
    }


    private static void Remove(string path, IEnumerable<string> fileNames)
    {
        foreach (var fileName in fileNames)
        {
            DirAccess.RemoveAbsolute($"{path}/{fileName}.json");
        }
    }

    //帮我转换成GDScript语言
    private void Convert(string path, IReadOnlyCollection<string> fileNames)
    {
        var spriteFrames = new SpriteFrames();
        foreach (var fileName in fileNames)
        {
            Json load = null;
            if (ResourceLoader.Exists($"{path}/{fileName}.json"))
            {
                load = (Json)GD.Load($"{path}/{fileName}.json");
            }
            else
            {
                GD.Print($"{path}/{fileName}.json  资源不存在");
            }

            if (load == null)
            {
                continue;
            }

            var spriteBean =
                JsonConvert.DeserializeObject<SpriteBean>(
                    Json.Stringify(load.Data));
            var names = _nameBy8;
            if (spriteBean.Group == 4)
            {
                names = _nameBy4;
            }

            spriteFrames.RemoveAnimation("default");
            for (var y = 0; y < spriteBean.Group; y++)
            {
                var animationName = $"{fileName}-{names[y]}";
                spriteFrames.AddAnimation(animationName);
                spriteFrames.SetAnimationSpeed(animationName, spriteBean.Frame);
                for (var x = 0; x < spriteBean.Frame; x++)
                {
                    var frame = (AtlasTexture)GetAtlasTexture($"{path}/{fileName}.png").Duplicate();
                    frame.Region = new Rect2(x * spriteBean.Width, y * spriteBean.Height, spriteBean.Width,
                        spriteBean.Height);
                    spriteFrames.AddFrame(animationName, frame);
                }
            }
        }

        ResourceSaver.Save(spriteFrames, $"{path}/default.tres", ResourceSaver.SaverFlags.ReplaceSubresourcePaths);
    }

    private static AtlasTexture GetAtlasTexture(string path)
    {
        var aTexture = new AtlasTexture();
        aTexture.Atlas = GD.Load<Texture2D>(path);
        return aTexture;
    }
}

public class SpriteBean
{
    public string Flag { get; set; }
    public int HLen { get; set; }
    public int Group { get; set; }
    public int Frame { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public int KeyX { get; set; }
    public int KeyY { get; set; }
}